﻿#include <ecal/ecal.h>
#include <ecal/msg/protobuf/server.h>

#include <thread>

#include "protocol/echo_service.pb.h"

class EchoServiceImpl : public example::EchoService {
 public:
  void Echo(::google::protobuf::RpcController* /*controller*/, const ::example::EchoRequest* request,
            ::example::EchoResponse* response, ::google::protobuf::Closure* /*done*/) override {
    std::cout << "Received request: " << request->message() << std::endl;
    response->set_reply("Processed: " + request->message());
    response->set_success(true);
  }
};

int main(int argc, char** argv) {
  std::string server_name = "Echo Service";
  if (argc > 1) {
    server_name = argv[1];
  }

  // 初始化 eCAL
  eCAL::Initialize(argc, argv, "Echo Server");

  // 创建服务端
  auto echo_service = std::make_shared<EchoServiceImpl>();
  eCAL::protobuf::CServiceServer<example::EchoService> echo_server(echo_service, server_name);

  // 保持服务运行
  while (eCAL::Ok()) {
    std::this_thread::sleep_for(std::chrono::milliseconds(100));
  }

  eCAL::Finalize();
  return 0;
}